package com.apobates.jforum2.troll.forum.stub.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.PatternSyntaxException;

/**
 * 版块组/卷
 */
public class BoardGroupVo implements Serializable{
    private int id;
    //名称
    private String title;
    //描述
    private String description;
    //创建日期
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime entryDateTime;
    //图标
    private String imageAddr;
    //显示顺序
    private int ranking;
    // 状态,false(0)禁用,true(1)可用
    private boolean status;
    // 访问地址
    private String link;
    public static Function<Integer,String> produceLink = (Integer boardgroupId)->{
        return String.format("/board/volumes/%d.xhtml", boardgroupId);
    };
    
    public BoardGroupVo() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getEntryDateTime() {
        return entryDateTime;
    }

    public void setEntryDateTime(LocalDateTime entryDateTime) {
        this.entryDateTime = entryDateTime;
    }

    public String getImageAddr() {
        return imageAddr;
    }

    public void setImageAddr(String imageAddr) {
        this.imageAddr = imageAddr;
    }

    public int getRanking() {
        return ranking;
    }

    public void setRanking(int ranking) {
        this.ranking = ranking;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public static Optional<BoardGroupVo> parseLink(String link){
        String[] params = {};
        try {
            params = link.split("-");
        } catch (NullPointerException | PatternSyntaxException e) {
            //throw new IllegalStateException("构造版块组的ActiveDirectoryConnector实例参数不合法");
        }
        if (params.length != 1) {
            //throw new IllegalStateException("构造版块组的ActiveDirectoryConnector实例失败");
            return Optional.empty();
        }
        BoardGroupVo boardGroup = new BoardGroupVo();
        int id = -1;
        try{
            id = Integer.valueOf(params[0]);
        }catch(NumberFormatException e){}
        boardGroup.setId(id);
        //boardGroup.setId(Commons.stringToInteger(params[0], -1));
        return Optional.of(boardGroup);
    }
    
    public static BoardGroupVo defaultInstance() { //getDefault
        BoardGroupVo bgv = new BoardGroupVo();
        bgv.setId(0);
        bgv.setTitle("默认版块组");
        bgv.setDescription("no description");
        
        bgv.setImageAddr("image://defat/ico/blank_icon.png");
        bgv.setRanking(0);
        bgv.setStatus(true);
        bgv.setLink("#");
        return bgv;
    }
}